<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        $this->createAdminPermissionsTable();
        $this->createAdminRolesTable();
        $this->createAdminRoleAdminTable();
        $this->createAdminRolePermissionTable();
    }

    public function down(): void
    {
        Schema::dropIfExists('admin_role_permission');
        Schema::dropIfExists('admin_role_admin');
        Schema::dropIfExists('admin_roles');
        Schema::dropIfExists('admin_permissions');
    }

    private function createAdminPermissionsTable(): void
    {
        Schema::create('admin_permissions', function (Blueprint $table) {
            $table->id();
            $table->string('name', 50)->unique()->comment('权限名称');
            $table->string('slug', 100)->unique()->comment('权限标识');
            $table->string('http_method', 20)->nullable()->comment('HTTP方法');
            $table->string('http_path')->nullable()->comment('HTTP路径');
            $table->unsignedBigInteger('parent_id')->default(0)->comment('父级ID');
            $table->integer('order')->default(0)->comment('排序');
            $table->string('icon', 50)->nullable()->comment('图标');
            $table->tinyInteger('status')->default(1)->comment('状态：0=禁用，1=启用');
            $table->timestamps();
            $table->softDeletes();
        });
    }

    private function createAdminRolesTable(): void
    {
        Schema::create('admin_roles', function (Blueprint $table) {
            $table->id();
            $table->string('name', 50)->unique()->comment('角色名称');
            $table->string('slug', 100)->unique()->comment('角色标识');
            $table->text('description')->nullable()->comment('角色描述');
            $table->tinyInteger('status')->default(1)->comment('状态：0=禁用，1=启用');
            $table->timestamps();
            $table->softDeletes();
        });
    }

    private function createAdminRoleAdminTable(): void
    {
        Schema::create('admin_role_admin', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('role_id')->comment('角色ID');
            $table->unsignedBigInteger('admin_id')->comment('管理员ID');
            $table->timestamps();

            $table->foreign('role_id')->references('id')->on('admin_roles')->onDelete('cascade');
            $table->foreign('admin_id')->references('id')->on('admin')->onDelete('cascade');
            $table->unique(['role_id', 'admin_id']);
        });
    }

    private function createAdminRolePermissionTable(): void
    {
        Schema::create('admin_role_permission', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('role_id')->comment('角色ID');
            $table->unsignedBigInteger('permission_id')->comment('权限ID');
            $table->timestamps();

            $table->foreign('role_id')->references('id')->on('admin_roles')->onDelete('cascade');
            $table->foreign('permission_id')->references('id')->on('admin_permissions')->onDelete('cascade');
            $table->unique(['role_id', 'permission_id']);
        });
    }
}; 